﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for clsConntion
/// </summary>
public class clsConnection
{
    private const string _strConn = "Data Source=MEODOVTTT-PC;Initial Catalog=news;Integrated Security=True;";
    protected SqlConnection _sqlConn;
    
    public clsConnection()
	{
        connectDatabase();
	}
    public void connectDatabase()
    {
        _sqlConn = new SqlConnection();
        try
        {
            _sqlConn.ConnectionString = _strConn;
            if (_sqlConn.State != System.Data.ConnectionState.Closed)
                _sqlConn.Close();
            _sqlConn.Open();
        }
        catch
        {
            _sqlConn.Dispose(); //Giai phong vung nho cua lop
        }
    }
    public void free() //Giai phong vung nho
    {
        _sqlConn.Dispose(); //Giai phong vung nho cua lop
    }
    public Boolean runSQL(string sql)
    {
        try {
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = _sqlConn;
            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.CommandText = sql;  //Insert, Update, Delete
            return(sqlComm.ExecuteNonQuery()!=0);
        }
        catch {
            return false;
        }
    }
    public Single runReturnSQL(string sql)
    {
        try
        {
            SqlCommand sqlComm = new SqlCommand();
            sqlComm.Connection = _sqlConn;
            sqlComm.CommandType = System.Data.CommandType.Text;
            sqlComm.CommandText = sql;  //Insert, Update, Delete
            return (Convert.ToSingle(sqlComm.ExecuteScalar()));
        }
        catch
        {
            return 0;
        }
    }
}
